import string
import random

import tkinter as tk
from time import strftime, localtime
from mylib.timer import ThreadTimer


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.listbox = tk.Listbox(self)
        self.listbox.insert("end", "one")
        self.listbox.insert("end", "two")
        self.listbox.insert("end", "three")
        self.listbox.bind("<Double-Button-1>", self.OnDouble)
        self.listbox.pack(side="top", fill="both", expand=True)

        # self.timer = MyTimer(0.1, self.on_timer)
        # self.timer.start()
        self.timer = ThreadTimer(0.1, self.on_timer)
        self.timer.start()

    def on_timer(self):
        tm = strftime('%Y-%m-%d %T> ', localtime())
        line = ''.join(random.choice(string.ascii_lowercase) for _ in range(11))
        self.listbox.insert("end", tm + line)
        while self.listbox.size() > 25:
            self.listbox.delete(0)

    def OnDouble(self, event):
        widget = event.widget
        selection = widget.curselection()
        value = widget.get(selection[0])
        print("selection:", selection, ": '%s'" % value)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()